package com.shuqi.common;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class FunctionsSwitch {
    
    /** 检查推广更新与否 */
    public static boolean isOfferWallUpdate(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return settings.getBoolean("isOfwUpdate", false);
    }
    
    /** 设置推广更新与否 */
    public static void setOfferWallUpdate(Context context, boolean isUpdate){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean("isOfwUpdate",isUpdate);
        editor.commit();
    }
    
    /** 检查推广更新时间 */
    public static String getOfferWallUpdateDate(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return settings.getString("ofwUpdateDate", "");
    }
    
    /** 设置推广更新时间 */
    public static void setOfferWallUpdateDate(Context context, String date){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putString("ofwUpdateDate",date);
        editor.commit();
    }
    
    /** 检查推广开关 */
    public static boolean isOfferWallOpen(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return PropertiesUtils.isOfferwallSwitchLocalOpen(context, false) && settings.getBoolean("offerWall", false);
    }

    /**通知栏更新功能： 查询开关 */
    public static boolean isNotificationOpen(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return settings.getBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_NOTIFICATIONREMIND, true);
    }
    /**通知栏更新功能： 打开 */
    public static void openNotification(Context context){
        controlNotification(context, true);
    }
    /**通知栏更新功能： 关闭*/
    public static void closeNotification(Context context){
        controlNotification(context, false);
    }
    private static void controlNotification(Context context, boolean isOpen){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_NOTIFICATIONREMIND,isOpen);
        editor.commit();
    }
    
    /**开启软件打开书架： 查询开关 */
    public static boolean isFirstOpenShelf(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return settings.getBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_OPENSHELF, false);
    }
    /**开启软件打开书架： 打开 */
    public static void openFirstOpenShelf(Context context){
        controlFirstOpenShelf(context, true);
    }
    /**开启软件打开书架： 关闭*/
    public static void closeFirstOpenShelf(Context context){
        controlFirstOpenShelf(context, false);
    }
    private static void controlFirstOpenShelf(Context context, boolean isOpen){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_OPENSHELF,isOpen);
        editor.commit();
    }
    
    /**检查是否创建了快捷方式*/
    public static boolean isCreatedShorCut(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        return settings.getBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_CREATEDSHORCUT, false);
    }
    /**创建快捷方式完成*/
    public static void createShorCutFinish(Context context){
        SharedPreferences settings = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean(SharedPreferenceUtil.SP_CONFIG_BOOLEAN_CREATEDSHORCUT,true);
        editor.commit();
    }
    
    /**
     * 检查是否为新安装或者更新后，第一次打开软件
     * @param context
     * @return
     */
    public static boolean isUpdateOrNewInstall(Context context){
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_TIPS, Context.MODE_PRIVATE);
        Log4an.e("Loading", "检查是否为新安装|升级：打开前："+sp.getString(SharedPreferenceUtil.SP_TIPS_STRING_VERSIONCHANGE, "")
                +", 当前："+Config.VERSION_INFO);
        return !Config.VERSION_INFO.equals(sp.getString(SharedPreferenceUtil.SP_TIPS_STRING_VERSIONCHANGE, ""));
    }
    /**
     * 更新保存的版本数据
     * @param context
     * @return
     */
    public static boolean updateAndSaveVersion(Context context){
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_TIPS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(SharedPreferenceUtil.SP_TIPS_STRING_PREVERSION, sp.getString(SharedPreferenceUtil.SP_TIPS_STRING_VERSIONCHANGE, ""));
        editor.putString(SharedPreferenceUtil.SP_TIPS_STRING_VERSIONCHANGE, Config.VERSION_INFO);
        return editor.commit();
    }
    /***
     * 对比是否为目标版本号之前更新来的
     * @param context
     * @param targetVersion
     * @return
     */
    public static boolean isUpdateBefore(Context context, String targetVersion){
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_TIPS, Context.MODE_PRIVATE);
        return (targetVersion.compareTo(sp.getString(SharedPreferenceUtil.SP_TIPS_STRING_VERSIONCHANGE, Config.VERSION_INFO)))> 0;
    }
    
    /**是否显示过新版介绍页面*/
    public static boolean isShowedNewbieGuide(Context context){
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_TIPS, Context.MODE_PRIVATE);
        return sp.getBoolean(SharedPreferenceUtil.SP_TIPS_BOOLEAN_SHOWEDNEWVERSIONTIP, false);
    }
    
    /**设置：显示/未显示 过新版介绍*/
    public static boolean showedNewbieGuide(Context context,boolean isShowed){
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_TIPS, Context.MODE_PRIVATE);
        return sp.edit().putBoolean(SharedPreferenceUtil.SP_TIPS_BOOLEAN_SHOWEDNEWVERSIONTIP, isShowed).commit();
    }
    
    /** weibkit、离线书籍管理底部弹框：是否显示气泡 */
    public static boolean isShowDownloadHint(Context context) {
        SharedPreferences settings =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_DOWNLOAD_HINT,
                        Context.MODE_PRIVATE);
        return settings.getBoolean("isShowHint", false);
    }

    /** weibkit、离线书籍管理底部弹框：是否显示气泡 */
    public static void setShowDownloadHint(Context context, boolean isUpdate) {
        SharedPreferences settings =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_DOWNLOAD_HINT,
                        Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean("isShowHint", isUpdate);
        editor.commit();
    }
}
